package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.ReportMapper;
import com.sky.service.ReportService;
import com.sky.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ReportServiceImpl  implements ReportService {

    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 营业额统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = getBetweenDates(begin, end);
        //使用Mybatis中的foreach的unionAll 实现多条sql语句的拼接，拼接日期字符串和金额字符串
        List<BigDecimal> amountList = reportMapper.getAmountsByDates(dateList);

        return TurnoverReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .turnoverList(StringUtils.join(amountList, ","))
                .build();
    }
    /**
     * 用户统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = getBetweenDates(begin, end);
        //每日新增用户数量集合
        List<Integer> newUserList = reportMapper.getNewUsersByDates(dateList);

        //总用户数量集合
        List<Integer> totalUserList = reportMapper.getTotalUsersByDates(dateList);
        return UserReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .newUserList(StringUtils.join(newUserList, ","))
                .totalUserList(StringUtils.join(totalUserList, ","))
                .build();
    }
    /**
     * 订单统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public OrderReportVO ordersStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = getBetweenDates(begin, end);
        //订单总数
        Integer orderCount = orderMapper.countAllOrders();
        //有效订单数(状态为已完成的订单)
        Integer validOrderCount = orderMapper.countStatus(Orders.COMPLETED);
        //订单完成率
        Double orderCompletionRate = validOrderCount / orderCount.doubleValue();
        //每日订单数
        List<Integer> orderCountList = reportMapper.getOrderCountByDates(dateList);
        //每日有效订单数
        List<Integer> validOrderCountList = reportMapper.getValidOrderCountByDates(dateList);
        return OrderReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .orderCountList(StringUtils.join(orderCountList, ","))
                .validOrderCountList(StringUtils.join(validOrderCountList, ","))
                .totalOrderCount(orderCount)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .build();
    }

    @Override
    public SalesTop10ReportVO top10(LocalDate begin, LocalDate end) {
        List<GoodsSalesDTO> list = reportMapper.getSalesTop10(begin, end);
        List<String> name = list.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        //使用stream流实现
        List<Integer> number = list.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());
        return SalesTop10ReportVO.builder()
                .nameList(StringUtils.join(name, ","))
                .numberList(StringUtils.join(number, ","))
                .build();

    }

    private List<LocalDate> getBetweenDates(LocalDate begin, LocalDate end) {
        List<LocalDate> result = new ArrayList<>();
        result.add(begin);
        while (!begin.equals(end)) {
            // 日期加一
            begin = begin.plusDays(1);
            result.add(begin);
        }
        return result;
    }
}
